﻿using System.Collections.Generic;
using System.Linq;
using Best.SmartCms.Dao.Common;
using Best.SmartCms.Domain;
using Best.SmartCms.Domain.Security;

namespace Best.SmartCms.Dao
{
    public class BusinessSystemDao : BaseDao<BusinessSystem>, ICommonDao
    {
        private readonly MySqlDbContext _db;

        public BusinessSystemDao(MySqlDbContext dbContext) : base(dbContext)
        {
            _db = dbContext;
        }

        /// <summary>
        /// 根据编号查找
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public BusinessSystem FindByCode(string code)
        {
            return _db.BusinessSystemSet.FirstOrDefault(p => p.Code == code);
        }

        /// <summary>
        /// 根据公钥查找
        /// </summary>
        /// <param name="accessKey"></param>
        /// <returns></returns>
        public BusinessSystem FindByAccessKey(string accessKey)
        {
            return _db.BusinessSystemSet.FirstOrDefault(p => p.AccessKey == accessKey);
        }

        /// <summary>
        /// 获取私钥
        /// </summary>
        /// <param name="accessKey"></param>
        /// <returns></returns>
        public string GetSecretByAccessKey(string accessKey)
        {
            var entity= _db.BusinessSystemSet.FirstOrDefault(p => p.AccessKey == accessKey);
            return entity == null ? "" : entity.Secret;
        }

        

        /// <summary>
        /// 获取所有系统信息
        /// </summary>
        /// <returns></returns>
        public List<BusinessSystem> GetAllBusinessSystems()
        {
            return _db.BusinessSystemSet.ToList();
        }
    }
}
